//
//  FirstViewController.m
//  HandWork
//
//  Created by 王阳阳 on 16/1/18.
//  Copyright © 2016年 www.lanou3g.com 蓝鸥科技. All rights reserved.
//

#import "FirstViewController.h"
#import "FirstModel.h"
#import "UIColor+colorChange.h"
#import "ClassifyView.h"
#import "ContentTableViewCell.h"
#import "MyLoopsView.h"
#import "ContentViewController.h"

@interface FirstViewController ()<UITableViewDataSource, UITableViewDelegate>
{
    NSInteger _page;//加载变量
}
@property (nonatomic, retain)NSMutableArray *collectionDataArr;//小collectionView和类似轮播图的CollectionView的数组
@property (nonatomic, retain) UIScrollView *scrollView;
@property (nonatomic, retain) UIView *bgView;//collectionView上头部区域的的View
@property (nonatomic, retain) UICollectionReusableView *header;//collectionView的头部区域
@property (nonnull, retain) NSMutableArray *lunboARR;//轮播图数组
@property (nonatomic, retain) UITableView *tableview;//内容Tablebiew
@property (nonatomic, retain) NSMutableArray *tabArr;//tableview内容数据数组

@end
@implementation FirstViewController

- (void)dealloc
{
 
    [_collectionDataArr release];
    [_scrollView release];
    [_bgView release];
    [_header release];
    [_lunboARR release];
    [_tableview release];
    [_tabArr release];
    
    [super dealloc];
}


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];//VC的背景颜色
    
    self.title = @"首页";//导航栏名称
  
  
    
    
    // 导航栏button
//    UIImage *barButtonImage = [ThemeManage shareThemeManage].isNight ? [UIImage imageNamed:@"yejian"] : [UIImage imageNamed:@"rijian"];
    
//    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithImage:barButtonImage style:UIBarButtonItemStyleDone target:self action:@selector(rightBarButtonAction:)];
    self.navigationController.navigationBar.tintColor = [UIColor blackColor];
    
 
    //数据解析
    [self data];
    //tableview数据
    [self tableViewdata];
    self.tableview = [[UITableView alloc]initWithFrame:self.view.frame style:UITableViewStylePlain];
    //代理
    self.tableview.delegate = self;
    self.tableview.dataSource = self;
    //添加
    [self.view addSubview:self.tableview];
    //重用池
    [self.tableview registerClass:[ContentTableViewCell class] forCellReuseIdentifier:@"tabcell"];
    [_tableview release];
    
    
    [self creatUI];//刷新加载
}
#pragma mark - tableView必须实现的两个方法
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _tabArr.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    ContentTableViewCell *tabcell = [tableView dequeueReusableCellWithIdentifier:@"tabcell" forIndexPath:indexPath];
    FirstModel *f = _tabArr[indexPath.row];//Model
    [tabcell.bgimg sd_setImageWithURL:[NSURL URLWithString:f.class_image]];//图片
    tabcell.titleLabel.text = f.subject;//标题
    return tabcell;
}
#pragma mark - tableview行高
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 200;
}
#pragma mark - tableview数据解析
- (void)data
{
    NSString *str = @"http://m.shougongke.com/index.php?&c=index&a=indexnew&vid=15";
   [AFNnetWork GETwithURL:str completion:^(id result) {
       //轮播图  图片数据
       NSArray *arr1 = result[@"data"][@"slide"];
       self.collectionDataArr = [NSMutableArray array];
       for (NSDictionary *dic in arr1) {
           [self.collectionDataArr addObject: dic[@"host_pic"]];
       }
       //热门内容的collectionView头部的区域
       [self creat];

       MyLoopsView *scroll = [MyLoopsView myLoopsViewWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 180) imageArr:_collectionDataArr];
       scroll.block = ^(NSInteger index){
  
           
       };
//       //把轮播图添加到头部view上
       [self.bgView addSubview:scroll];

       [_tableview reloadData];//刷新
   }];
    [self.tableview.mj_header endRefreshing];
}
#pragma mark - tableview的数据
- (void)tableViewdata
{
    NSString *str = @"http://yuntuapi.amap.com/datasearch/local?tableid=550fa56ce4b050797967f047&city=全国&keywords=&filter=%2Bcity_name%3A北京%2B&sortrule=sort_index%3A0&limit=20&page=1&key=55f0eadf1e00f8defca60c0f4bb1e700&ts=1453345879719&scode=34a7629856b763c3cbb34faafb3cf4ad";
    NSString *urlstr = [str stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet characterSetWithCharactersInString:str]];
    [AFNnetWork GETwithURL:urlstr completion:^(id result) {
        NSArray *tabarr = result[@"datas"];
        self.tabArr = [FirstModel modelHandlerWithArray:tabarr];
        [_tableview reloadData];//刷新
 }];
}

-(void)creat
{
    //View
    self.bgView= [[UIView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 470)];
    [self.tableview addSubview:self.bgView];
    self.tableview.tableHeaderView = _bgView;//把view作为tableview 的头部区域
    //分类label
    UILabel *classLabel = [[UILabel alloc]initWithFrame:CGRectMake(10, 180 + 5, 80, 30)];
    classLabel.text = @"分类";
    [self.bgView addSubview:classLabel];
    [classLabel release];
    
    //分类的自定义View
    ClassifyView *classify = [[ClassifyView alloc]initWithFrame:CGRectMake(0,220, self.view.frame.size.width, 230)];
    [self.bgView addSubview:classify];
    [classify release];
   
    [_bgView release];
    
    
}
#pragma mark - 刷新
- (void)creatUI
{
    //刷新
    MJRefreshNormalHeader *header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        _page = 1;
        [self data];
    }];
    [header setTitle:@"我刷我刷我刷刷刷" forState:MJRefreshStatePulling];
    [header beginRefreshing];
    self.tableview.mj_header = header;
    //加载
    MJRefreshAutoFooter *footer = [MJRefreshAutoFooter footerWithRefreshingBlock:^{
        _page ++;//加载个数
        [self table];
        
    }];
    self.tableview.mj_footer = footer;
}
#pragma mark - 加载数据
- (void)table
{

    
    NSString *str = [[@"http://yuntuapi.amap.com/datasearch/local?tableid=550fa56ce4b050797967f047&city=%E5%8C%97%E4%BA%AC&keywords=&filter=%2Bline_type%3A1%2B&sortrule=sort_index%3A0&limit=20&"stringByAppendingString:[NSString stringWithFormat:@"page=%ld",_page] ] stringByAppendingString:@"&key=55f0eadf1e00f8defca60c0f4bb1e700&ts=1453980622016&scode=944e31ee08e192d4b4472f8d683946c0"];
    [AFNnetWork GETwithURL:str completion:^(id result) {
        NSArray *arr = result[@"datas"];
        NSArray *atemp = [FirstModel modelHandlerWithArray:arr];
        for (FirstModel *f in atemp) {
            [self.tabArr addObject:f];
        }
        [self.tableview reloadData];
    }];
    [self.tableview.mj_footer endRefreshing];

}

#pragma mark - 内容点击方法
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    //点击跳转新的页面
    ContentViewController *content = [[ContentViewController alloc]init];
     FirstModel *f = _tabArr[indexPath.row];
    content.mid = f.cid.stringValue ;
    [self.navigationController pushViewController:content animated:YES];
  
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
